package com.kingschan.jeasytag.tag.form;

import java.io.File;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import com.kingschan.jeasytag.container.EasyTagContainer;
import com.kingschan.jeasytag.container.impl.EasyTagContainerImpl;
import com.kingschan.jeasytag.core.annotations.tag.*;
import com.kingschan.jeasytag.core.parse.TagParseFactory;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.power.SQLTag;
import com.kingschan.util.bean.BeanUtil;
import com.kingschan.util.encryption.MD5;
import com.kingschan.util.lang.StringUtil;
import com.kingschan.util.web.ServletUtil;
/**
 * ComboBoxTag
 * @author kingschan
 *date:2013-4-18
 */
@TagMetaData(CssClass = "easyui-combobox", tag = "select"
	
)
public class ComboBoxTag extends RootTag{
	private static final long serialVersionUID = 1L;
	@NumberField(description="组件的宽度.")
	private String width;
	@NumberField(description="下拉 panel 宽度.")
	private String panelWidth;
	@NumberField(description="下拉 panel 高度.")
	private String panelHeight;
	@BooleanField(description="决定是否支持多项选择.")
	private String multiple;
	@StringField(description="多项选择的时候的文本分隔符.")
	private String separator;
	@BooleanField(description="用户是否可以直接输入文本到选择框.")
	private String editable;
	@BooleanField(description="是否禁用该选择框.")
	private String disabled;
	@BooleanField(description="定义是否显示下拉箭头按钮.")
	private String hasDownArrow;
	@StringField(description="默认值.")
	private String value;
	@NumberField(description="从最后一次输入事件,延时查找.")
	private String delay;
	@EventField(args="",description="当下拉panel显示的时候触发")
	private String onShowPanel;
	@EventField(args="",description="当下拉panel隐藏的时候触发.")
	private String onHidePanel;
	@EventField(description="当选择框的值发生改变的时候触发",args="newValue,oldValue")
	private String onChange;
	
	
	@StringField(description="绑定到这个combobox的基础数据值名")
	private String valueField;
	@StringField(description="绑定到这个combobox的数据字段名.")
	private String textField;
	@StringField(description="当文本改变时定义如何加载显示列表数据.如果设置为'remote' combobox从远程服务器加载数据.当设置为'remote'模式时, 用户输入的信息将发送作为http请求参数,参数名为'q'到服务器检索新的数据. ")
	private String mode;
	@StringField(description="一个从远程服务器加载列表数据的URL.")
	private String url;
	@StringField(description="设置为auto则自动生成URL添加sqltag标签自动加载数据。      检索数据的http请求方法.")
	private String method;
	@StringField(description="需要加载到列表的数据.",appendFlag="")
	private String data;
	@EventField(description="定义如何过滤本地数据,当'mode'设置为'local'的时候. 这个函数提供两个参数:q: 用户输入的文本.row: 列表的行数据.返回true 允许行显示 ",args="")
	private String filter;
	@EventField(description="定义如何呈现行. 这个函数提供一个参数 :row. ",args="row")
	private String formatter;
	@EventField(description="定义如何从远程服务器加载数据. 返回false终止这个动作.这个函数提供一下参数:param:传递给远程服务器的参数对象.success(data): 当检索数据成功这个回调函数将被调用.error():当检索数据失败(异常,而不是返回空数据的时候)这个函数将被调用.",args="param,success,error")
	private String loader;
	@EventField(description="一个请求在加载数据之前触发,返回false取消这个加载动作",args="param")
	private String onBeforeLoad;
	@EventField(description="当远程数据加载成功之后触发.",args="")
	private String onLoadSuccess;
	@EventField(description="远程数据加载出错触发.",args="")
	private String onLoadError;
	@EventField(description="当用户选择一个列表项的时候触发.",args="record")
	private String onSelect;
	@EventField(description="当用户取消选择一个列表项的时候触发.",args="record")
	private String onUnselect;
	
	
	
	
	private StringBuffer html;
	private SQLTag sqltag;
	//private static Logger log = Logger.getLogger(ComboBoxTag.class);
	private EasyTagContainer container=EasyTagContainerImpl.getInstance();
	@Override
	public int doStartTag() throws JspException {
		html= new StringBuffer();	
		try {
			//如果没有显示指定url值 URL值则为默认值
			if ("auto".equals(StringUtil.ConvertNullStr(this.url))) {
				this.url=ServletUtil.getApplictionRootUrl(pageContext).concat("/jeasytag/combobox")
				.concat("?target=").concat(new MD5().getMD5ofStr(this.getId()+ServletUtil.getRequestURI(pageContext)))		
				;
			}		
			html.append(TagParseFactory.executeParse(this,pageContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	@Override
	public int doAfterBody() throws JspException {		
		html.append(this.getBodyContent().getString().toString().replaceAll("\t|\r|\n", ""));		
		return super.doAfterBody();
	}
	
	@Override
	public int doEndTag() throws JspException {				
		html.append("</select>");
		try {
			this.pageContext.getOut().write(html.toString());
			if (null!=this.getSqltag()) {			
				String filename=new MD5().getMD5ofStr(this.getId()+ServletUtil.getRequestURI(this.pageContext)).concat(".data");
				String path=container.getSerializationPath().concat(filename);				
					if(!new File(path).exists()){
						BeanUtil.serializationObject(path, this.getSqltag());			
					}			
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		return super.doEndTag();
	}
	
	
	
	
	
	
	
	
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getPanelWidth() {
		return panelWidth;
	}
	public void setPanelWidth(String panelWidth) {
		this.panelWidth = panelWidth;
	}
	public String getPanelHeight() {
		return panelHeight;
	}
	public void setPanelHeight(String panelHeight) {
		this.panelHeight = panelHeight;
	}
	public String getMultiple() {
		return multiple;
	}
	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}
	public String getSeparator() {
		return separator;
	}
	public void setSeparator(String separator) {
		this.separator = separator;
	}
	public String getEditable() {
		return editable;
	}
	public void setEditable(String editable) {
		this.editable = editable;
	}
	public String getDisabled() {
		return disabled;
	}
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}
	public String getHasDownArrow() {
		return hasDownArrow;
	}
	public void setHasDownArrow(String hasDownArrow) {
		this.hasDownArrow = hasDownArrow;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getDelay() {
		return delay;
	}
	public void setDelay(String delay) {
		this.delay = delay;
	}
	public String getOnShowPanel() {
		return onShowPanel;
	}
	public void setOnShowPanel(String onShowPanel) {
		this.onShowPanel = onShowPanel;
	}
	public String getOnHidePanel() {
		return onHidePanel;
	}
	public void setOnHidePanel(String onHidePanel) {
		this.onHidePanel = onHidePanel;
	}
	public String getOnChange() {
		return onChange;
	}
	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}
	public String getValueField() {
		return valueField;
	}
	public void setValueField(String valueField) {
		this.valueField = valueField;
	}
	public String getTextField() {
		return textField;
	}
	public void setTextField(String textField) {
		this.textField = textField;
	}
	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public String getFilter() {
		return filter;
	}
	public void setFilter(String filter) {
		this.filter = filter;
	}
	public String getFormatter() {
		return formatter;
	}
	public void setFormatter(String formatter) {
		this.formatter = formatter;
	}
	public String getLoader() {
		return loader;
	}
	public void setLoader(String loader) {
		this.loader = loader;
	}
	public String getOnBeforeLoad() {
		return onBeforeLoad;
	}
	public void setOnBeforeLoad(String onBeforeLoad) {
		this.onBeforeLoad = onBeforeLoad;
	}
	public String getOnLoadSuccess() {
		return onLoadSuccess;
	}
	public void setOnLoadSuccess(String onLoadSuccess) {
		this.onLoadSuccess = onLoadSuccess;
	}
	public String getOnLoadError() {
		return onLoadError;
	}
	public void setOnLoadError(String onLoadError) {
		this.onLoadError = onLoadError;
	}
	public String getOnSelect() {
		return onSelect;
	}
	public void setOnSelect(String onSelect) {
		this.onSelect = onSelect;
	}
	public String getOnUnselect() {
		return onUnselect;
	}
	public void setOnUnselect(String onUnselect) {
		this.onUnselect = onUnselect;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public SQLTag getSqltag() {
		return sqltag;
	}

	public void setSqltag(SQLTag sqltag) {
		this.sqltag = sqltag;
	}
	
	
	
}
